package com.lion.tag.input;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.lion.tag.LionInputTag;
import com.lion.tag.util.AppendUtil;

public class NumberBox extends LionInputTag {
	private Integer precision;// 保留小数位(数字2,表示保留两位)
	private String groupSeparator;// 单位分隔符
	private String prefix;// 前缀
	private String suffix;// 后缀
	private String onChange;
	private Double max;// 最大值
	private Double min;// 最小值
	private boolean spinner = false;// 可微调节
	private Double increment;// 微调节步长

	/**
	 * 实现数字输入框
	 */
	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();
		StringBuilder sb = new StringBuilder(200);
		sb.append("<input ");
		AppendUtil.addAttribute(sb, "id", id);
		if (name != null && !name.equals("")) {
			AppendUtil.addAttribute(sb, "name", name);
		}
		boolean isSpinner = false;
		if (!spinner) {
			sb.append("class=\"easyui-numberbox\" ");
		} else {
			sb.append("class=\"easyui-numberspinner\" ");
			isSpinner = true;
		}
		if (required) {
			sb.append("required ");
		}
		sb.append("data-options=\"");
		int hasData = 0;
		if (precision != null && !precision.equals("")) {
			hasData = 1;
			sb.append("precision:");
			sb.append(precision);
		}
		if (min != null && !min.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("min:");
			sb.append(min);
			sb.append("");
		}
		if (max != null && !max.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("max:");
			sb.append(max);
			sb.append("");
		}
		if (groupSeparator != null && !groupSeparator.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("groupSeparator:'");
			sb.append(groupSeparator);
			sb.append("'");
		}
		if (prefix != null && !prefix.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("prefix:'");
			sb.append(prefix);
			sb.append("'");
		}
		if (suffix != null && !suffix.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("suffix:'");
			sb.append(suffix);
			sb.append("'");
		}
		if (isSpinner) {
			if (increment == null) {
				increment = 1d;
			}
			sb.append("increment:");
			sb.append(increment);
			sb.append("");
		}

		sb.append("\" ");
		AppendUtil.addAttribute(sb, "value", value);
		AppendUtil.addAttribute(sb, "onChange", onChange);
		AppendUtil.addAttribute(sb, "style", style);
		sb.append("/>");
		out.write(sb.toString());
	}

	/**
	 * @return the max
	 */
	public Double getMax() {
		return max;
	}

	/**
	 * @param max
	 *            the max to set
	 */
	public void setMax(Double max) {
		this.max = max;
	}

	/**
	 * @return the min
	 */
	public Double getMin() {
		return min;
	}

	/**
	 * @param min
	 *            the min to set
	 */
	public void setMin(Double min) {
		this.min = min;
	}

	/**
	 * @return the id
	 */
	@Override
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	@Override
	public void setId(String id) {
		this.id = id;
	}

	public boolean isSpinner() {
		return spinner;
	}

	public void setSpinner(boolean spinner) {
		this.spinner = spinner;
	}

	public Double getIncrement() {
		return increment;
	}

	public void setIncrement(Double increment) {
		this.increment = increment;
	}

	/**
	 * @return the required
	 */
	public boolean isRequired() {
		return required;
	}

	/**
	 * @param required
	 *            the required to set
	 */
	public void setRequired(boolean required) {
		this.required = required;
	}

	/**
	 * @return the precision
	 */
	public Integer getPrecision() {
		return precision;
	}

	/**
	 * @param precision
	 *            the precision to set
	 */
	public void setPrecision(Integer precision) {
		this.precision = precision;
	}

	/**
	 * @return the groupSeparator
	 */
	public String getGroupSeparator() {
		return groupSeparator;
	}

	/**
	 * @param groupSeparator
	 *            the groupSeparator to set
	 */
	public void setGroupSeparator(String groupSeparator) {
		this.groupSeparator = groupSeparator;
	}

	/**
	 * @return the prefix
	 */
	public String getPrefix() {
		return prefix;
	}

	/**
	 * @param prefix
	 *            the prefix to set
	 */
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * @return the suffix
	 */
	public String getSuffix() {
		return suffix;
	}

	/**
	 * @param suffix
	 *            the suffix to set
	 */
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	/**
	 * @return the onChange
	 */
	public String getOnChange() {
		return onChange;
	}

	/**
	 * @param onChange
	 *            the onChange to set
	 */
	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	/**
	 * @return the style
	 */
	@Override
	public String getStyle() {
		return style;
	}

	/**
	 * @param style
	 *            the style to set
	 */
	@Override
	public void setStyle(String style) {
		this.style = style;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

}
